/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Iterator;
import java.util.ListIterator;
import javax.swing.border.Border;
import main.WinkelApplication;
import model.Klant;

/**
 *
 * @author Jack Sierkstra
 */
public class CustomerPanel extends JPanel {   
    
    JTable table;
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    int klantid;
    
    public CustomerPanel() {
      super();
      setLayout(new BorderLayout());
      addTitle();
      table();
      addButtonPane();
    }    
    public void table() {
         
    
        
        String[] columnames = {"Klant ID","Naam","Adres", "Postcode", "Woonplaats"};
  
        java.util.List<Klant> customers = WinkelApplication.getQueryManager().getCustomers();
        Object[][] data = new Object[customers.size()][];
        
        for (int i = 0; i < customers.size(); i++)
        {
            Klant k = customers.get(i);
            
            data[i] = new Object[] { k.getKlant_id(),k.getNaam(), k.getAdres(), k.getPostcode(),k.getWoonplaats()};
        }
        
        table = new JTable(data, columnames) {
            @Override
            public boolean isCellEditable(int row, int column){  
                return false;  
            }  
        };
        table.setPreferredScrollableViewportSize(new Dimension((dim.width - 50), (dim.height - 40)));
        table.setRowHeight(25);
        table.setFont(WinkelApplication.FONT_14_PLAIN);
        table.setFillsViewportHeight(true);
        
        
        JScrollPane scrollpane = new JScrollPane(table);
       
        add(scrollpane, BorderLayout.CENTER);
        
        table.addMouseListener(new MouseAdapter() {
            @Override
          public void mouseClicked(MouseEvent e) {
           
              JTable target = (JTable)e.getSource();
              int row = table.getSelectedRow();
              //int col = table.getSelectedColumn();
              
              
              Object lastobj = table.getModel().getValueAt(row, 0);
              Integer last = new Integer(lastobj.toString());
              // do some action if appropriate column
              klantid = last;
            
          }
        });
        
        
    }
    
    public void customerRemove(int klantid) {
        
        
        if(klantid > 0) {
            int response = JOptionPane.showConfirmDialog(
                        null,
                        "Weet je zeker dat je deze klant wilt verwijderen?",
                        "Verwijder klant",
                        JOptionPane.YES_NO_OPTION
         );          
            if (response == JOptionPane.YES_OPTION )
                    {
                    WinkelApplication.getQueryManager().removeCustomer(klantid);
                     WinkelApplication.getInstance().showPanel(new view.CustomerPanel());
                    JOptionPane.showMessageDialog(null,
                    "De klant is verwijderd!","Verwijderd",
                    JOptionPane.INFORMATION_MESSAGE);
                    } 
        
        WinkelApplication.getInstance().showPanel(new view.CustomerPanel());
        }
        if (klantid == 0 || klantid == -1) {
            JOptionPane.showMessageDialog(null,
            "Er moet een klant geselecteerd zijn om te verwijderen.","Selecteer een klant",
            JOptionPane.INFORMATION_MESSAGE);  
        }
    }
     
    public void customerEdit(int klantid) {
        if(klantid > 0) {
            JFrame frame = new JFrame();
            frame.add(new view.CustomerEdit(klantid));
            frame.setResizable(false);
            frame.setLocation((dim.width/2)-230, (dim.height/2)-230);
            frame.setSize(430, 250);
            frame.setVisible(true);
            
        } if (klantid == 0 || klantid == -1) {
            JOptionPane.showMessageDialog(null,
            "Er moet een klant geselecteerd zijn om te wijzigen.","Selecteer een klant",
            JOptionPane.INFORMATION_MESSAGE);  
        }
        
    }

    private void addButtonPane() {
        
        JPanel frame = new JPanel();
        frame.setPreferredSize(new Dimension(dim.width, 60));
        
        JButton toevoegen = new JButton("Nieuwe klant");
        
        toevoegen.setPreferredSize(new Dimension(150,50));
        toevoegen.addMouseListener(new MouseAdapter() {
          
          @Override
          public void mouseClicked(MouseEvent e) {
              
             JFrame frame = new JFrame();
             frame.add(new view.CustomerAdd());
             frame.setResizable(false);
             frame.setLocation((dim.width/2)-230, (dim.height/2)-230);
             
             frame.setSize(430, 230);
             frame.setVisible(true);
             
          }
        });
        
        JButton terug = new JButton("Terug naar beheer");
        
        terug.setPreferredSize(new Dimension(150,50));
        terug.addMouseListener(new MouseAdapter() {
          
          @Override
          public void mouseClicked(MouseEvent e) {   
             WinkelApplication.getInstance().showPanel(new view.Manage(WinkelApplication.getInstance().getAccesLevel()));
          }
        });
        
        JButton wijzig = new JButton("Wijzig");
        wijzig.setAlignmentX(LEFT_ALIGNMENT);
        wijzig.setPreferredSize(new Dimension(150,50));
        wijzig.addMouseListener(new MouseAdapter() {
          
          @Override
          public void mouseClicked(MouseEvent e) {   
              customerEdit(klantid);

          }
        });
        
        JButton verwijder = new JButton("Verwijder");
        verwijder.setAlignmentX(LEFT_ALIGNMENT);
        verwijder.setPreferredSize(new Dimension(150,50));
        verwijder.addMouseListener(new MouseAdapter() {
          
          @Override
          public void mouseClicked(MouseEvent e) {   
            
                    customerRemove(klantid);

          }
        });
        
        frame.add(toevoegen);
        frame.add(wijzig);
        if(WinkelApplication.getInstance().getAccesLevel()==3) frame.add(verwijder);
        frame.add(terug);
        
        add(frame, BorderLayout.PAGE_END);
        
    }

    private void addTitle() {
        JLabel titel = new JLabel("Klanten beheren", SwingConstants.CENTER);
        titel.setFont(WinkelApplication.getInstance().computeFontSize(2));
        
        JPanel frame = new JPanel();
        frame.setLayout(new BoxLayout(frame, BoxLayout.Y_AXIS));
        
        
        frame.add(titel);
        
        titel.setAlignmentX(Component.CENTER_ALIGNMENT);
       
        add(frame, BorderLayout.PAGE_START);
    }
    
   
}